<?php
/**
 * Created by PhpStorm.
 * User: xiao
 * Date: 2017/7/31
 * Time: 14:06
 */
namespace excel;

class Excel
{
    public function createExcel($array,$data,$name,$foot = false)
    {
        $objPHPExcel = new  \PHPExcel();
        $objProps    = $objPHPExcel->getProperties();

        $count=count($array);

//        for ($i = 1; $i < $count; $i++) {
//            if ($i > 1) {
//                $objPHPExcel->createSheet();
//            }

        $objPHPExcel->setActiveSheetIndex(0);//把当前创建的sheet设置为活动sheet
        $objSheet = $objPHPExcel->getActiveSheet();//获得当前活动Sheet
        $objSheet->setTitle("user" . 1);
        $objSheet->getDefaultStyle()->getFont()->setSize(14);

        $cell_array=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T'];
        foreach($array as $k=>$v){
            $cell=$cell_array[$k].'1';
            if(is_array($v)){
                $objSheet->getColumnDimension($cell_array[$k])->setWidth($v['1']);
                $objSheet->setCellValue($cell,$v['0']);
            }else{
                $objSheet->setCellValue($cell,$v);
            }
        }

        $j = 2;
        foreach ($data as $key => $value) {

            foreach($value as $k=>$v){
                $objSheet ->getStyle($cell_array[$k]. $j)->getAlignment()->setWrapText(true);
                $objSheet->setCellValue($cell_array[$k]. $j, $v);
            }
            $j++;
        }
        if($foot){
          //  $objSheet->mergeCells('A1'.$j.':H1'.$j);
            $objSheet->setCellValue('A'. $j, $foot);
        }
        // }

        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");
        header('Content-Disposition:attachment;filename='.$name.'.xls');
        header("Content-Transfer-Encoding:binary");
        $objWriter->save('php://output');
        exit;
    }
}